﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ChineseSchool.BLL
{
    public class B_Insert_Update_Delete
    {
        private PetaPoco.Database db;
        public B_Insert_Update_Delete()
        {
            db = new PetaPoco.Database("ConnectionString");
        }

        //Insert object
        public void insert(string tenbang, string primaryKeyName, object obj)
        {
            try
            {
                var id = db.Insert(tenbang, primaryKeyName, obj);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.CloseSharedConnection();
            }
        }

        //Insert object and return id
        public int InsertReturn(string tenbang, string primaryKeyName, object obj)
        {
            try
            {
                int id = (int)db.Insert(tenbang, primaryKeyName, obj);
                return id;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.CloseSharedConnection();
            }
        }

        //Update Object with primaryKey Value
        public void update(string tenbang, string primaryKeyName, object obj, int primaryKeyValue)
        {
            try
            {
                db.Update(tenbang, primaryKeyName, obj, primaryKeyValue);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                db.CloseSharedConnection();
            }
        }

        //xóa 1 dòng bất bì ở các bảng
        public object delete(string tableName, string primaryKeyName, object obj, object primaryKeyValue)
        {
            return db.Delete(tableName, primaryKeyName, obj, primaryKeyValue);
        }
    }
}